<?php
class AssetsController extends AppController {
	
	var $name = 'Assets';
	var $layout = 'admin';
	var $components = array('SimpleUpload');
	
	/**
	 *  The AuthComponent provides the needed functionality
	 *  for login, so you can leave this function blank.
	 */
	function beforeFilter() {
		
		$this->Auth->loginAction = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginRedirect = array ('controller' => 'contents', 'action' => 'index' );
		$this->Auth->logoutRedirect = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginError = 'The information you\'ve entered is incorrect.';
		$this->Auth->logoutError = 'You\'ve successfully logged out.';
		$username = $this->Auth->user ();
		$this->set ( 'username', $username ['User'] ['name'] );
		$this->Auth->allow ( 'login', 'logout' );
		//echo  $this->Auth->password('a') ;6dd25d865031f6a39cc950ba6fedfbe26a9908b9 
	

	}
	
	public function index() {
		$params ['order'] = 'id';
		if (isset ( $this->params ['named'] ['orderBy'] )) {
			$params ['order'] = $this->params ['named'] ['orderBy'];
			$this->set ( 'orderBy', $this->params ['named'] ['orderBy'] );
			if (isset ( $this->params ['named'] ['DESC'] )) {
				$params ['order'] .= ' DESC';
				$this->set ( 'DESC', $this->params ['named'] ['DESC'] );
			}
		}
		$assets = $this->Asset->find ( 'all', $params );
		$this->set ( 'assets', $assets );
		
		
		
		$groups = $this->Asset->find ( 'all', array ('fields' => 'DISTINCT group') );
		$array ['-1'] = '--select--';
		$array ['-2'] = '--other--';
		
		foreach ( $groups as $group ) {
			$array [$group ['Asset'] ['group']] = $group ['Asset'] ['group'];
		}
		$this->set ( 'groups', $array );
		
		
		
		$this->set('pageTitle', $this->pageTitle = 'File Manager');
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->Asset->delete ( $id )) {
				$this->Session->setFlash ( 'File deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such file.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		if (! empty ( $this->data )) {
			$this->Asset->create ();
			
			$saveable = array(
				"name" => $this->data['Asset']['name'],
			    "group" => ($this->data ['Asset']['group'] == -2) ? $this->data['Asset']['group_other'] : $this->data ['Asset']['group'],
			);
			
			
			if($this->data['Asset']['image'] && $this->data['Asset']['image']['size']!=0){
				$fileOk	= $this->SimpleUpload->uploadFiles($this->Asset->save_path, array( $this->data['Asset']['image']));
				if(array_key_exists('errors', $fileOk))
				{
					$sErrorMessages = implode(',', $fileOk['errors']);
					$this->Session->setFlash( $sErrorMessages );
					return;
				}else if(array_key_exists('urls',$fileOk))
				{
					$saveable['url'] = $fileOk['urls'][0]; 
					$saveable['filename'] = $this->data['Asset']['image']['name']; 
				}
			}
			if ($this->Asset->save ( $saveable )) {
				$this->Session->setFlash ( 'File added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add file!' );
			}
		}
	}
	
}

?>